package com.vsd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dialet.PaginationSupport;
import com.demo.util.SqlUtils;
import com.vsd.daoEx.model.IndexActivityEx;
import com.vsd.domain.IndexActivity;
import com.vsd.repository.IndexActivityRepository;
import com.vsd.service.IndexActivityService;

@Service
@Transactional
public class IndexActivityServiceImpl implements IndexActivityService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(IndexActivityServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private IndexActivityRepository  indexActivityRepository;
	
	@Override
	public IndexActivity save(IndexActivity act){
		return indexActivityRepository.save(act);
	}

	@Override
	public List<IndexActivityEx> selectExList(String areaCode) {
		String sql = "select ia.*,c.name as cityName from f_index_activity ia "
				+ "	left join f_city c on ia.area_code = c.code where ia.state !=-1 ";
		
		if(StringUtils.isNotBlank(areaCode)){
			sql+=" and ia.area_code = '"+areaCode+"' order by ia.sort_order desc";
		}else{
			sql+=" order by ia.create_time desc,ia.sort_order desc";
		}
		
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<IndexActivityEx> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			IndexActivityEx ex = new IndexActivityEx();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		return exList;
	}
	
	@Override
	public List<IndexActivityEx> selectNowList(String areaCode,String storeId) {
		String sql = "select ia.*,c.name as cityName "
				+ ",(select count(1) from f_index_act_goods fiag left join s_store_goods sg1 on fiag.gid = sg1.id where sg1.store_id = '"+storeId+"' and  fiag.act_id = ia.id ) as goodsCnt"
				+ ",(select count(1) from f_index_act_store fias   where fias.store_id = '"+storeId+"' and  fias.act_id = ia.id ) as storeCnt"
				+ " from f_index_activity ia "
				+ "	left join f_city c on ia.area_code = c.code where ia.state !=-1 "
				+ "and  ia.state=1 order by ia.sort_order desc";
		 
		int c = jdbcTemplate.queryForObject("select count(1) from ("+ sql+") a_cnt", Integer.class);
		PaginationSupport.getContext().setTotalCount(c);
		
		sql+=" limit "+(PaginationSupport.getContext().getStartRowNo()-1)+","+PaginationSupport.getContext().getPageLength();
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<IndexActivityEx> exList = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			IndexActivityEx ex = new IndexActivityEx();
			SqlUtils.transMap2Bean2(map, ex);
			exList.add(ex);
		}
		return exList;
	}
	

	@Override
	public int updateByPrimaryKey(IndexActivity act) {
		String sql=null;
		try {
			sql = SqlUtils.update(act, true, " where id = '"+act.getId()+"'");
		} catch (Exception e) {
			return 0;
		}
		return jdbcTemplate.update(sql);
	}

	@Override
	public IndexActivity findOne(String id) {
		return indexActivityRepository.findOne(id);
	}

	@Override
	public void delById(String id) {
		indexActivityRepository.delete(id);
	}
	
 
	 

	
}
